#include "usergame.h"

#define FRAME_WIDTH       LCDWIDTH
#define FRAME_HEIGHT      (uint8_t)(LCDHEIGHT-8)

#define FLY_SIZE (uint8_t)8
#define FLY_POINT_SPACE  12               //每两个障碍点之间的像素空隙
#define FLY_POINT_NUM   FRAME_WIDTH/FLY_POINT_SPACE+1   //每十个x像素有一个点，后面添加两个增加一点难度
#define FLY_POINT_SIZE 2

#define MIN_LEVEL   1
#define MAX_LEVEL   3



static uint8_t level=1;
static uint16_t score=0;

static int16_t fly_x = FRAME_WIDTH/2; //set the horizontal position to the middle of the screen
static int16_t fly_y = FRAME_HEIGHT/2; //vertical position
static int8_t fly_vx = 2; //horizontal velocity
static int8_t fly_vy = 2; //vertical velocity

int16_t flyPointx[FLY_POINT_NUM];
int16_t flyPointy[FLY_POINT_NUM];

const static  uint8_t feiji[]= 
{
	8,8,
	0x10,0x18,0x8C,0xFF,0xFF,0x8C,0x18,0x10,
};

static const uint8_t game3logo[]=
{
64,16,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x80,0xE0,0x00,0x00,0x00,0x00,0x00,0x01,
0x80,0xF0,0x03,0x00,0xFF,0x9C,0x1E,0x1F,0xE0,0xF0,0x03,0x06,0xFF,0x9C,0x1E,0x3D,
0xE0,0xF8,0x00,0x06,0xFF,0x9C,0x0F,0x3D,0xE0,0xF8,0x00,0x00,0xF0,0x1C,0x0F,0xF9,
0xFF,0xFF,0x80,0x00,0xF0,0x1C,0x07,0xF1,0xFF,0xFF,0x80,0x00,0xFF,0x9C,0x03,0xF1,
0xFF,0xFF,0x80,0x00,0xFF,0x9C,0x03,0xE1,0xE0,0xF8,0x01,0x80,0xFF,0x9C,0x01,0xE1,
0xE0,0xF8,0x01,0x80,0xF0,0x1C,0x01,0xC1,0xE0,0xF8,0x00,0x00,0xF0,0x1C,0x01,0xC1,
0x80,0xF0,0x00,0x00,0xF0,0x1F,0xF1,0xC1,0x80,0xF0,0x00,0x00,0xF0,0x1F,0xF1,0xC1,
0x80,0xE0,0x0C,0x0C,0xF0,0x1F,0xF1,0xC1,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};

void game3Start(uint8_t cmd)
{
		gb.display.drawBitmap(9,0,game3logo);	
		gb.display.setCursor(25,25);
		gb.display.setFont(font3x5);
		gb.display.print(F("level:"));
		gb.display.print(level);
		gb.display.setCursor(4,38);
		gb.display.setFont(font5x7);
		gb.display.print(F("please start!"));
	  
		if((cmd==CMD_START)||(cmd==CMD_DOWN))
		{
			gb.gameState=GAME_STATE_RUN;
		}
		else if(cmd==CMD_LEFT)
		{
			if(level>MIN_LEVEL)
			{
				level--;
			}
		}
		else if(cmd==CMD_RIGHT)
		{
			if(level<MAX_LEVEL)
			{
				level++;
			}
		}
		else if(cmd==CMD_NUMBER)
		{
			if((cache[0]>=MIN_LEVEL)&&(cache[0]<=MAX_LEVEL))
			{
				level=cache[0];
			}
		}		
		fly_x = FRAME_WIDTH/2-FLY_SIZE/2;    //ball起始位置
		fly_y = FRAME_HEIGHT/2-FLY_SIZE/2; 
}

void  game3Reset()
{
		fly_x = FRAME_WIDTH/2-FLY_SIZE/2;    //ball起始位置
		fly_y = FRAME_HEIGHT/2-FLY_SIZE/2; 
		for(uint8_t i=0;i<FLY_POINT_NUM;i++)    //开始之前先初始化障碍点的位置
		{
			flyPointx[i]=FRAME_WIDTH+i*FLY_POINT_SPACE;     //每10个x像素有一个障碍点
			//根据当前时间值millis和i共同作用产生一个随机点
			flyPointy[i]=(i*i*99%13+i*76%67+millis()%73+millis()*23)%(FRAME_HEIGHT-FLY_POINT_SIZE);
		}
		score=0;   //计分清零
}
void game3Run(uint8_t cmd)
{
		static uint8_t timer;
	  static uint8_t upTimer;
	  static uint8_t step;
		timer++;
	  if(level==1)
		{
			upTimer=5;
			step=2;
		}
		else if(level==2)
		{
			upTimer=5;
			step=4;
		}
		else if(level==3)
		{
			upTimer=4;
			step=5;
		}

		if(timer>=upTimer)
		{
			timer=0;
			for(uint8_t i=0;i<FLY_POINT_NUM;i++)
			{
				flyPointx[i]-=step;
				if(flyPointx[i]<=0)
				{
					flyPointx[i]=FRAME_WIDTH+FLY_POINT_SPACE;					
					flyPointy[i]=(i*i*99%13+i*76%67+i*millis()%73+millis()*23)%(FRAME_HEIGHT-FLY_POINT_SIZE);
					score++;   //计分加1
				}
			}
		
		}
		
	
		//move the ball using the buttons
		if(cmd==CMD_UP)
		{ 
			fly_y = fly_y - fly_vy;	
		}
		if(cmd==CMD_DOWN)
		{
			fly_y = fly_y + fly_vy;					
		}
		if(cmd==CMD_LEFT)
		{
			fly_x = fly_x - fly_vx;			
		}
		if(cmd==CMD_RIGHT)
		{
			fly_x = fly_x + fly_vx; //increase the horizontal position by the ball's velocity		
		}	
		if(cmd==CMD_RETURN)
		{
			gb.gameState=GAME_STATE_RESTART;
		}
		if(cmd==CMD_RETURN) {
		gb.gameState=GAME_STATE_START;
		}
		//check that the ball is not going out of the screen
		//if the ball is touching the left side of the screen
		if(fly_x < 0){
		//bring it back in the screen
		fly_x = 0;
		}
		//if the ball is touching the right side
		if((fly_x + FLY_SIZE) > FRAME_WIDTH){
		fly_x = FRAME_WIDTH - FLY_SIZE;
		}
		//if the ball is touching the top side
		if(fly_y < 0){
		fly_y = 0;
		}
		//if the ball is touching the down side
		if((fly_y + FLY_SIZE) > FRAME_HEIGHT){
		fly_y = FRAME_HEIGHT - FLY_SIZE;
		}

		for(uint8_t i=0;i<FLY_POINT_NUM;i++)
		{
			gb.display.fillRect(flyPointx[i],flyPointy[i], FLY_POINT_SIZE, FLY_POINT_SIZE); //画障碍点
			//判断飞机是否碰到障碍点
			if(((flyPointx[i]+FLY_POINT_SIZE)>fly_x) && (flyPointx[i]<(fly_x+FLY_SIZE)))
			{
				if(((flyPointy[i]+FLY_POINT_SIZE)>fly_y)&&(flyPointy[i]<(fly_y+FLY_SIZE)))
				{
					gb.gameState=GAME_STATE_GAMEOVER;
					gb.popup(F("  GAME OVER!"),50);
					game3Reset();
				}
			}			
		}
		
		gb.display.drawFastHLine(0,FRAME_HEIGHT,FRAME_WIDTH);
		gb.display.drawBitmap(fly_x,fly_y,feiji);
		gb.display.setCursor(0,FRAME_HEIGHT+1);
		gb.display.print(F("score :"));
		gb.display.print(score);
}


int8_t Game::game3()
{	
	gameState=GAME_STATE_START;
	display.clear();
	display.persistence=false;   //不保留每一次绘画内容
	
	game3Reset();
	while(1)
	{
		if(update())
		{
			uint8_t tmpCmd=user_i2cGetCmd();			
			if(state!=DIS_STATE_GAME3)
			{
				display.clear();
				display.update();
				return 1;
			}
			switch(gameState)
			{
				case GAME_STATE_START:
					game3Start(tmpCmd);
					break;
				case GAME_STATE_RUN:
					game3Run(tmpCmd);
					break;
				case GAME_STATE_GAMEOVER:
					game3Start(tmpCmd);
					break;
			}
		}
	}

}

